/*
THIS FILE PERFORMS ANALYSES ON IDR APPLICANT DATA
- OPTIMAL BW ESTIMATES
- FORMAL RDD AND RDK ESTIMATES

AUTHOR: LT, TM
CREATED: 3/8/2023
LAST UPDATED: 9/13/2023 
*/

clear
set niceness 0
set segmentsize 3g
set more off 



gl original = ""
gl working  = ""
gl output   = ""

cd "${original}"


/*******************************************************************************/
/*******************************************************************************/
**# F1  IDR ENROLLMENT AND APPS, ALL NEW APPLICANTS
/*******************************************************************************/
/*******************************************************************************/
use "${working}\app", clear


** F1B: TIME SERIES OF APPLICATIONS
binscatter v1 v2 v3 if v3 >0, n(46) line(none) m(Oh O ) mc(midblue dknavy ) ///
	xti(Months since first application) xlab(0(2)46, labs(small)) ///
	yti(Percent of borrowers) ylab(, gmin gmax labs(small)) ///
	legend(lab(1 Any application in month) lab(2 Valid application in month) size(small) position(6) region(lcolor(black)) col(2)) ///
	graphr(margin(small) ) xsize(8)
graph save "${output}\F1Brecert_by_moel.gph", replace

** SIMILAR PATTERNS IF LIMIT TO ANALYSIS SAMPLE BW
keep if abs(v4)<4350
binscatter v1 v2 v3 if v3 >0, n(46) line(none) m(Oh O ) mc(midblue dknavy ) ///
	xti(Months since first application) xlab(0(2)46, labs(small)) ///
	yti(Percent of borrowers) ylab(, gmin gmax labs(small)) ///
	legend(lab(1 Any application in month) lab(2 Valid application in month) size(small) position(6) region(lcolor(black)) col(2)) ///
	graphr(margin(small) ) xsize(8)


** F1A: TIME SERIES OF IDR PARTICIPATION
tempfile temp

foreach n of num 1(1)46 {
	
	unzipfile "idrrd_moel_`n'.csv.zip" , replace
	insheet using "idrrd_moel_`n'.csv", names clear 
	erase "idrrd_moel_`n'.csv"

	merge m:1 v5 using "${working}\baseline", keep(1 3) nogen
	drop if v45 <500
	
	rename v6 v3
	merge 1:1 v5 v3 using "${working}\app", update 
	keep if _m>=3
		
	egen plan_icr = rowmax(v7 v8)
	egen plan_idr = rowmax(plan_icr v9 v10 v11 v12 v13)
	gen plan_idr_valid = plan_idr*v14

	keep v3 plan_idr_valid 
	
	collapse (mean) plan_idr_valid, by(moel)
	
	if `n'>1 {
		append using `temp'
	}
	
	save `temp', replace
	
}

use `temp', clear	
	
tw (scatter plan_idr_valid v3, ms(O) mc(dknavy) msize(medium)), ///
	xti(Months since first application) xlab(0(2)46, labs(small)) ///
	yti(Percent on IDR ) ylab(, gmin gmax labs(small)) ///
	ylab(0(.1)1, gmin gmax) legend(off) graphr(margin(small) ) xsize(8)

graph save "${output}\F1Av14_by_moel.gph", replace


/*******************************************************************************/
/*******************************************************************************/
**# T1  SAMPLE CHARACTERISTICS
/*******************************************************************************/
/*******************************************************************************/

* FAFSA CHARS
unzipfile     "idrrd_fafsa.csv.zip" , replace
insheet using "idrrd_fafsa.csv", names clear 
erase         "idrrd_fafsa.csv"

drop if v15 >2018

bysort v5 (v15 ): gen first = _n==1
gen dum = v16 == "2" if v16 ~=""
bysort v5: egen female = max(dum)
drop dum
gen miss_gender = female ==.
recode female (. =0)
keep if first == 1
recode v17 (9999999 = .)
gen miss_v17 = agi==.

* ADJUST FAMILY INCOME FOR INFLATION (JAN 2022 $)	
rename v15 year
gen month = 1
merge m:1 year month using "${working}\cpi", keep(1 3) nogen
loc num = 281.148
replace cpi = `num'/cpi
replace v17 = round(agi*cpi)

rename year v50

*keep female miss_gender v17 miss_v17 v21 v5 v50
tempfile fafsa
save `fafsa', replace

*******
**  Balance and scheduled payment prior to app
loc t -3
unzipfile     "idrrd_moel_`t'.csv.zip" , replace
insheet using "idrrd_moel_`t'.csv", names clear 
erase         "idrrd_moel_`t'.csv"

replace v18 = . if v19 != 1 
keep v5 v18 
tempfile prappsched
save `prappsched', replace 


loc t 0
unzipfile     "idrrd_moel_`t'.csv.zip" , replace
insheet using "idrrd_moel_`t'.csv", names clear 
erase         "idrrd_moel_`t'.csv"

keep v5 outst_bal
tempfile prappbal
save `prappbal', replace 

*****
* BASELINE CHARS FILE 
unzipfile     "idrrd_baseline_chars.csv.zip" , replace
insheet using "idrrd_baseline_chars.csv", names clear 
erase         "idrrd_baseline_chars.csv"

merge 1:1 v5 using `fafsa'
keep if inlist(_merge,1,3)
gen miss_fafsa = _merge==1
drop _merge

merge 1:1 v5 using `prappbal'
keep if inlist(_merge,1,3)
gen miss_prappbal = _merge==1
drop _merge

merge 1:1 v5 using `prappsched'
keep if inlist(_merge,1,3)
gen miss_prappsched = _merge==1
drop _merge

sum miss*


* AGE AT v1 
gen v49 = age(mdy(real(substr(v21,6,2)),1,real(substr(v21,1,4))), mdy(real(substr(v22,6,2)),1,real(substr(v22,1,4))))

* MISSING AGI
replace v17 = . if v50>v23
replace miss_v17 = 1 if v17 ==.

* ACADEMIC LEVEL INDICATORS
xi, noomit prefix(A) i.v24

* AUTO DEBT 6 MONTHS BEFORE APP
gen v55 = v25 ==1


g rpyrs = (mdy(real(substr(v22,6,2)),real(substr(v22,9,2)),real(substr(v22,1,4))) ///
          - mdy(real(substr(v26,6,2)),real(substr(v26,9,2)),real(substr(v26,1,4)))) ///
		  /365
g rpyrs_2 = rpyrs >= 2 if !mi(rpyrs)

g grad = Amax_stud_a_6 == 1

* 
* dependency status
g dependent     = inlist(v27,1,4)
g indep_wnodep  = inlist(v27,2,5)
g indep_withdep = inlist(v27,3,6)

g fullneeds_test = inlist(v27,1,2,3)
	drop v27

* parental education
foreach p in mth fth {
	g `p'ed_nohs = `p'r_hgh_grd_lvl == 1 if !mi(`p'r_hgh_grd_lvl)
	g `p'ed_hs   = `p'r_hgh_grd_lvl == 2 if !mi(`p'r_hgh_grd_lvl) 
	g `p'ed_coll = `p'r_hgh_grd_lvl == 3 if !mi(`p'r_hgh_grd_lvl)
	g `p'ed_unkn = inlist(`p'r_hgh_grd_lvl,4,.) if !mi(`p'r_hgh_grd_lvl)	
	replace `p'r_hgh_grd_lvl = . if `p'r_hgh_grd_lvl == 4
}

g max_pared = max(v28,v29)
g max_pared_mi = mi(max_pared)
g max_pared_nohs = max_pared == 1
g max_pared_hs   = max_pared == 2
g max_pared_coll = max_pared == 3

drop v28 v29

* clean FAFSA income 
replace v17 = .        if inlist(v17,-9999999,-999999,999999,9999999)
replace stu_v17 = .    if inlist(v30,-9999999,-999999,999999,9999999)
replace parent_v17 = . if inlist(v31,-9999999,-999999,999999,9999999)
*replace v17 = 0 if v17 < 0

* Savings and Wealth
local swvars v32 v33 v34 ///
			 v35 v36 v37
foreach v of local swvars {
	replace `v' = . if inlist(`v',-9999999,-999999,999999,9999999)
}

g nz_savings_stu =  v32 > 0 & !mi(v32)
g nz_invest_stu  = (v33 > 0 & !mi(v33)) | ///
				   (v34 > 0 & !mi(v34))  
				   
g nz_savings_par =  v35   > 0 & !mi(v35)
g nz_invest_par  = (v36 > 0 & !mi(v36)) | ///
				   (v37 > 0 & !mi(v37)) 
				   
g nz_assets_par = nz_savings_par == 1 | nz_invest_par == 1
g nz_assets_stu = nz_savings_stu == 1 | nz_invest_stu == 1

egen assets_par = rowtotal(v35 v36 v37) , m
egen assets_stu = rowtotal(v32 v33 v34) , m

drop `swvars'
				   
* Degree sought
g deg_bach  = inlist(v38,1,2)   if !mi(v38)
g deg_assoc = inlist(v38,3,4)   if !mi(v38)
g deg_cert  = inlist(v38,5,6,7) if !mi(v38)
g deg_grad  = v38 == 8          if !mi(v38)
g deg_oth   = v38 == 9          if !mi(v38)

g       degrc = 1 if inlist(v38,1,2) 
replace degrc = 2 if inlist(v38,3,4)
replace degrc = 3 if inlist(v38,5,6,7)
replace degrc = 4 if v38 == 8 
replace degrc = 5 if v38 == 9    

label define ffdlbl 1 "Bachelor" 2 "Assoc" 3 "Cert" 4 "Graduate" 5 "Other"    
lab val degrc ffdlbl

drop v38

******************************
*
cap drop month cpi 
gen month = real(substr(v22,6,2))
cap ren v23 year
merge m:1 year month using "${working}\cpi", keep(1 3) nogen

g cpi_d =281.148/cpi // JAN 2022 $

g d_inc_real     = round(v4*cpi_d)
g inc_real       = round(v39*cpi_d)
g sched_amt_real = round(v18*cpi_d)
g outst_bal_real = round(v20*cpi_d)


g _20k_bw_samp = abs(d_inc_real) <= 20000
g _4k_bw_samp = abs(d_inc_real) <= 4350

count
count if v40
count if v41
count if _20k_bw_samp
count if _4k_bw_samp

*****************************************
*** SUMMARY STATS TABLE


local sumvars outst_bal_real sched_amt_real inc_real v42 v43 grad rpyrs_2 v44 /// 
              dependent v17 female 
			  
label var outst_bal_real "Outstanding balance"
label var sched_amt_real "Scheduled monthly payment"
label var inc_real       "Income"
label var v42        "Household Size"
label var v43        "married"
label var grad           "Graduate loans"
label var rpyrs_2        "Years since repayment entry >= 2"
label var v44     "Prior default"
label var dependent      "Dependent"
label var v17            "Family AGI"
label var female         "Female"
label var max_pared_nohs "Parent ed. att. no HS"
label var max_pared_hs   "Parent ed. att. HS"
label var max_pared_coll "Parent ed. att. college"
			  
eststo clear 
eststo: estpost summarize `sumvars' 
eststo: estpost summarize `sumvars' if _4k_bw_samp  == 1

esttab using "$output\T1descriptives.csv", replace ///
cells("mean") label nodepvar nonum collabels(none) ///
prehead("Summary Statitistics of RD Sample") ///
mlabels("All first-time IDR applicants" "$4350 bandwidth") ///
refcat(v39_real "IDR application information" ///
	   grad "Borrower Characteristics" ///
	   dependent "First FAFSA Characteristics")


/*******************************************************************************/
/*******************************************************************************/
**# T2, F2, TA1, FA4  PLACEBO ESTIMATES 
/*******************************************************************************/
/*******************************************************************************/

unzipfile "${working}\baseline.dta.zip", replace

use "${working}\baseline",clear

* SAMPLE SELECTION
drop if v45 < 500

* COMMENT OUT TO KEEP SERVICER 2 
drop if v46 == 2

gen servicer_rc = v46 
recode servicer_rc (6 8 9 10 11 12 33 37 46 . = 999) (43 = 555)
xi, noomit prefix(SVC) i.servicer_rc
drop SVCservicer_999 

foreach v of var A* {
	recode `v' (. = 0)
}

egen inc_src_tax = rowmax(v47 v48)
recode v43 (. = 0)

foreach v of var v49 female v17 v50 {
	recode `v' (0 = .) if miss_fafsa == 1
}

replace female = . if miss_gender == 1

gen Y2015 = v23 == 2015

gen miss_race = v51 ==.

** T2, TA1: PLACEBO ESTIMATES
loc j = 0

foreach v of var v49 female v17 v51 v52 v53 A* ///
	v42 v43 inc_src_tax v54 v45 v44 v55   {

loc j = `j'+1
	

	reg `v' v56 v57 v4 if v58  ==0 & abs(v4)<4.35, robust
	sum `v' if e(sample) ==1 & d==0
	loc m = round(r(mean),.0001)
	
	loc ap = "ap"
	if `j'==1 {
		loc ap = "replace"
	}
	outreg2 using "${output}\T2baseline_placebo.txt", keep(v56 v57) `ap' ///
		addstat("depvar mean",`m',"BW", 4350) aster(se) 10pct 

* CLOSE LOOP OVER BASELINE VARS
}


* ANY DEF/DEL/FORB IN 12 MONTHS PRIOR TO INITIAL APP
merge 1:1 v5 using "${working}\collapse", nogen keep(1 3)

egen any_forb_pre   = rowmax(v65 v66 v67 v68 v69 v70) 
egen any_delinq_pre = rowmax(v71 v72)
egen any_def_pre    = rowmax(v73 v74)
	

* INDEX OF ALL OBSERVABLE BASELINE CHARS - PREDICTED PR(DEFAULT BY END OF YEAR 2)
egen ever_deflt_y2 = rowmax(v75 v76 v77 v78) ///
	if v77 ~=. & v78 ~=.

foreach v of var v49 female v17 v50 {
	recode `v' (. = 0) if miss_fafsa ==1
}

recode female (. = 0) if miss_gender ==1
recode v17 (. = 0) if miss_v17 == 1
recode v49 (. =0 )
foreach v of var phat_* {
	recode `v' (. = 0)
}

reg ever_deflt_y2 v44 A* Y* ///
	v51 v52 v53 miss_race ///
	v45 v42 v43 v48 v47 v54 SVC*  v55 ///
	v49 miss_fafsa miss_gender female v17 miss_v17 v50

predict index


reg index v56 v57 v4 if v58 == 0 & abs(v4)<4.35, robust
sum index if e(sample) ==1 & v56==0
loc m = round(r(mean),.0001)
	
outreg2 using "${output}\T2baseline_placebo.txt", keep(v56 v57) ap ///
	addstat("depvar mean",`m',"BW", 4350) aster(se) 10pct 

	
** SCATTER PLOTS

* 6K BW TO CONSTRUCT BINS
replace v4  = v4*1000
keep if abs(v4)<=6500

* $250 BINS
gen bin = round(v4 +124, 250) - 125

* DEAL WITH MISSING VALUES FOR FAFSA, RACE VARS
foreach v of var phat_* {
	recode `v' (0 = .) if miss_race ==1
}


foreach v of var v49 female v17 v50 {
	recode `v' (0 = .) if miss_fafsa == 1
}

recode female (0 = .) if miss_gender ==1
recode v17 (0 = .) if miss_v17 ==1

rename Y2015 Yappyr_2015


preserve

	gen num = 1
		
	collapse (mean) index  (sum) num Y* , by(bin)

** F2: COUNTS BY YEAR OF APPLICATION 
	forv i = 2015/2018 {
	tw 	(sc     Yappyr_`i' bin , mc(dknavy)) ///
		(lfitci Yappyr_`i' bin if bin < 0  & bin >-4250, ciplot(rline) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
		(lfitci Yappyr_`i' bin if bin >125 & bin <4250, ciplot(rline ) clc(black) alc(gs10) alp(longdash) alw(thin))  ///
		if bin~=125 & abs(bin)<6000, ///
		xli(0, lc(black) lp(shortdash)) xlab(-6000(1000)6000, labs(small)) xti(Distance from 150% FPL Threshold) ///
		 ylab(, gmin gmax) yti(Number of applicants) ///
		 ti("`i' Applicants")legend(off) graphr(fc(white) margin(small)) 
	 graph save "${output}\F2scatter_count_v23`i'_nosvc2.gph", replace

	 * CLOSE LOOP
	}

** FA4: INDEX OF BASELINE CHARS
	foreach v of var index {
		
		tw (sc  `v' bin , mc(dknavy)) ///
		(lfitci `v' bin if bin < 0  & bin >-4250, ciplot(rline) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
		(lfitci `v' bin if bin >125 & bin<4250, ciplot(rline ) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
		if bin~=125 & abs(bin)<6000, ///
		xli(0, lc(black) lp(shortdash)) xlab(-6000(1000)6000, labs(small)) xti(Distance from 150% FPL Threshold) ///
		ylab(.075(.005).095, gmin gmax) ///
		legend(off) yti("") graphr(fc(white) margin(small)) 
	 graph save "${output}\FA4scatter_baseline_`v'.gph", replace
	
	* CLOSE LOOP
	}

restore
	
	
/*******************************************************************************/
/*******************************************************************************/
**# F8, FA8: SCATTER PLOTS FOR RECERTIFICATION, ON IDR 
/*******************************************************************************/
/*******************************************************************************/

unzipfile "${working}\app.dta.zip", replace

* TREATMENT EFFECTS ON RECERTIFICATION 
* USE COLLAPSED DATA
use "${working}\collapse",clear

merge m:1 v5 using "${working}\baseline", keep(1 3) nogen

* SAMPLE SELECTION
drop if v45 < 500
drop if v46 == 2	
	
* $250 BINS
replace v4 = v4*1000
gen bin = round(v4 +124, 250) - 125	

keep if abs(v4)<6500

gen in_samp = v59 ~=. | v60 ~=.
keep if in_samp == 1
	
preserve

	gen num = 1

	collapse (mean) pct_idr_* any_idr_* any_app_* any_valid_app_* (sum) num, by(bin)
	
	forv i = 3/5 {
		
		loc ylab3 = ".87(.01).92"
		loc ylab4 = ".46(.01).51"
		loc ylab5 = ".52(.01).57"
		

		tw 	(sc any_idr_`i' bin , mc(gold) ms(S) msiz(medium)) ///
			(lfitci any_idr_`i' bin if bin<0 & bin >-4250, ciplot(rline) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
			(lfitci any_idr_`i' bin if bin >125 & bin<4250, ciplot(rline ) clc(black) alc(gs10) alp(longdash) alw(thin))   ///
			if bin~=125 & abs(bin)<6000, ///
			xli(0, lc(black) lp(shortdash)) xlab(-6000(1000)6000, labs(small) nogrid) xti(Distance from 150% FPL Threshold) ///
			yti("") ylab(`ylab`i'', gmin gmax) ///
			legend(off) graphr(fc(white))  xsize(6.25)
		 
	 graph save "${output}\FA8scatter_any_idr_`i'.gph", replace
	
	* CLOSE LOOP OVER OUTCOMES
	}
	
	forv i = 3/4 {
		
		loc ylab3 = ".26(.01).3"
		loc ylab4 = ".21(.01).25"
	
		tw 	(sc any_valid_app_`i' bin , mc(red) ms(X) msiz(medlarge)) ///
			(lfitci any_valid_app_`i' bin if bin<0 & bin >-4250, ciplot(rline) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
			(lfitci any_valid_app_`i' bin if bin >125 & bin<4250, ciplot(rline ) clc(maroon) alc(gs10) alp(longdash) alw(thin)) ///
			if bin~=125 & abs(bin)<6000, ///
			xli(0, lc(black) lp(shortdash)) xti(Distance from 150% FPL Threshold)   xlab(-6000(1000)6000, labs(small) nogrid) ///
			yti("") ylab(`ylab`i'', gmin gmax) ///
			graphr(fc(white)) legend(off )  xsize(6.25)
	 
		graph save "${output}\F8scatter_valid_app_`i'.gph", replace
		
		loc ylab3 = ".31(.01).35"
		loc ylab4 = ".24(.01).28"
		
		tw	(sc any_app_`i' bin, mc(orange) ms(Oh) msiz(medium)) ///
			(lfitci any_app_`i' bin if bin<0 & bin >-4250, ciplot(rline) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
			(lfitci any_app_`i' bin if bin >125 & bin<4250, ciplot(rline) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
			if bin~=125 & abs(bin)<6000, ///
			xli(0, lc(black) lp(shortdash)) xlab(-6000(1000)6000, labs(small) nogrid) xti(Distance from 150% FPL Threshold) ///
			yti("") ylab(`ylab`i'', gmin gmax) ///
			legend(off) graphr(fc(white) margin(small)) xsize(6.25)
	 graph save "${output}\F8scatter_apps_`i'.gph", replace
	
	* CLOSE LOOP OVER OUTCOMES
	}
	
restore


erase "${working}\app.dta"


/*******************************************************************************/
/*******************************************************************************/
**# OUTPUT FOR F7: MONTH BY MONTH ESTIMATES FOR IN IDR PLAN
/*******************************************************************************/
/*******************************************************************************/

unzipfile "${working}\app.dta.zip", replace

foreach n of num 1(1)46 {
	
	unzipfile     "idrrd_moel_`n'.csv.zip" , replace
	insheet using "idrrd_moel_`n'.csv", names clear 
	erase         "idrrd_moel_`n'.csv"

* INFLATION ADJ RUNNING VAR
	drop v4 v58
	merge 1:1 v5 using "${working}\treatment", keep(3) nogen

	merge m:1 v5 using "${working}\baseline", keep(1 3) nogen

* SAMPLE SELECTION
	drop if v45 <500
	drop if v46 == 2

* BANDWIDTH	
	keep if abs(v4)<4350	
	
* PUT RUNNING VAR IN $1K TERMS	
	replace v4  = v4/1000
	replace v57 = v57/1000

	rename v6 v3
	merge 1:1 v5 v3 using "${working}\app", update 
	keep if _m>=3
		
	egen plan_icr = rowmax(v7 v8)
	egen plan_idr = rowmax(plan_icr v9 v10 v11  v12 v13 )
	gen plan_idr_valid = plan_idr*v14
		
	drop if v58==1
	
	loc controls = "Yappyr_2016 Yappyr_2017 Yappyr_2018"
	
	foreach v of var plan_idr_valid {

		reg `v' v56 v57 v4  `controls' , robust
		
		loc ap = "ap"
		
		if `n' == 1  {
			loc ap = "replace"
		}
		
		outreg2 using "${output}\monthly_`v'.txt", keep(v56 v57) `ap' addtext("parm",coef_`n') st(coef ) noaster nonotes nopa nocons nor 
		outreg2 using "${output}\monthly_`v'.txt", keep(v56 v57) ap addtext("parm",cil_`n') st(ci_low ) noaster nonotes nopa nocons nor 
		outreg2 using "${output}\monthly_`v'.txt", keep(v56 v57) ap addtext("parm",cih_`n') st(ci_high ) noaster nonotes nopa nocons nor 
	
	}

	
* CLOSE LOOP OVER MONTHS SINCE INITIAL APP
}

erase "${working}\app.dta"


/*******************************************************************************/
/*******************************************************************************/
**# OUTPUT FOR FA11: MONTH BY MONTH ESTIMATES FOR ENROLLMENT, GRADUATION SINCE APP
/*******************************************************************************/
/*******************************************************************************/

unzipfile "${working}\enrl.dta", replace
unzipfile "${working}\grad.dta", replace

loc start = 0

foreach n of num `start'(1)46 {
	
	unzipfile "idrrd_moel_`n'.csv.zip" , replace
	insheet using "idrrd_moel_`n'.csv", names clear 
	erase "idrrd_moel_`n'.csv"

*INFLATION ADJ RUNNING VAR
	drop v4 v58
	merge 1:1 v5 using "${working}\treatment", keep(3) nogen
			
	merge m:1 v5 using "${working}\baseline", keep(1 3) nogen

* SAMPLE SELECTION
	drop if v45 <500
	drop if v46 == 2
	
* BANDWIDTH	
	keep if abs(v4)<4350

* PUT RUNNING VAR IN $1K TERMS	
	replace v4  = v4/1000
	replace v57 = v57/1000
	
	rename v6 v3
	merge 1:1 v5 v3 using "${working}\enrl", update 
	drop if _m==2
	drop _m
	
	merge 1:1 v5 v3 using "${working}\grad", update 
	drop if _m==2
	drop _m
		
	recode grad_in_mo (. = 0 )
	recode any_enrl (. = 0)
	recode anygrad (. = 0)
	
	drop if v58 == 1
	
	loc controls = "Yappyr_2016 Yappyr_2017 Yappyr_2018"

	foreach v of var any_enrl anygrad {

		reg `v' v56 v57 v4  `controls' , robust
		
		loc ap = "ap"
		
		if `n' == `start'  {
			loc ap = "replace"
		}
		
		outreg2 using "${output}\monthly_`v'.txt", keep(v56 v57) `ap' addtext("parm",coef_`n') st(coef ) noaster nonotes nopa nocons nor 
		outreg2 using "${output}\monthly_`v'.txt", keep(v56 v57) ap addtext("parm",cil_`n') st(ci_low ) noaster nonotes nopa nocons nor 
		outreg2 using "${output}\monthly_`v'.txt", keep(v56 v57) ap addtext("parm",cih_`n') st(ci_high ) noaster nonotes nopa nocons nor 
	
	}

	
* CLOSE LOOP OVER MONTHS SINCE INITIAL APP
}

erase "${working}\enrl.dta"
erase "${working}\grad.dta"


/*******************************************************************************/
/*******************************************************************************/
**# OUTPUT FOR F4C, F5B, F6C, F9, F10C, FA10
*** F3A-B, F4A-B, F5A, F6A-B, F10A-B, FA5, FA6, FA7: FS/RF SCATTER PLOTS
*** MONTH BY MONTH ESTIMATES FOR PAYMENT AMOUNTS, DELINQ/DEF/FORB, BALANCE
/*******************************************************************************/
/*******************************************************************************/

/*********************************************************************
	* 1) OUTPUT FOR F6, F7, F8C, F9C, F10C
*********************************************************************/
loc start = -12
foreach n of num `start'(1)46 {

* SERVICER ID AND AUTO DEBIT IND
	unzipfile     "idrrd_evar1_moel`n'.csv.zip" , replace
	insheet using "idrrd_evar1_moel`n'.csv", names clear 
	erase         "idrrd_evar1_moel`n'.csv"	
	keep v5 v89 v88
	tempfile temp
	save `temp', replace

* MAIN MONTHLY FILE
	unzipfile     "idrrd_moel_`n'.csv.zip" , replace
	insheet using "idrrd_moel_`n'.csv", names clear 
	erase         "idrrd_moel_`n'.csv"

	merge 1:1 v5 using `temp', keep(3) nogen
	
	capture drop cpi
	
* ADJUST SCHEDULED PAYMENT AMOUNT FOR INFLATION
	gen month = real(substr(partdate,6,2))
	gen year  = real(substr(partdate,1,4))
	
	merge m:1 year month using "${working}\cpi", keep(3) nogen
	
* JAN 2022 $	
	loc num = 281.148
	
	replace cpi = `num'/cpi
		
	foreach v of var v18 v20  {
		replace `v' = round(`v'*cpi)
	}
	
* WINSORIZE MONTHLY SCHEDULED PAYMENT AT 99TH PERCENTILE
	sum v18, det
	replace v18 = r(p99) if v18>r(p99) & v18<.	

* INFLATION ADJ RUNNING VAR
	drop v4 v58
	merge 1:1 v5 using "${working}\treatment", keep(3) nogen
	
* BASELINE CHARS
	merge m:1 v5 using "${working}\baseline", keep(1 3) nogen

* BANDWIDTH	
	keep if abs(v4)<4350
	
* SAMPLE SELECTION
	drop if v45 <500
	drop if v46 ==2
	
* PUT RUNNING VAR IN $1K TERMS	
	replace v4  = v4/1000
	replace v57 = v57/1000

* INCREASE IN OUTSTANDING DEBT (NOMINAL) SINCE BASELINE
	egen dum = rowtotal(v91 v92)
	gen bal_increase = round(dum,100) > round(outst_bal_nom0,100)
		
* ANY FORB
	egen any_forb = rowtotal(v79 v80 v81) if v79 ~=. | v80 ~= . | v81 ~= . 
	
* RECODE MISSING SCHEDULED AMOUNT IF IN STATUSES WE KNOW HAVE $0 PAYMENT	
	recode v18 (. = 0) if v82 == 1 | v83 == 1 | v84 == 1 | any_forb ==1  ///
		| v84 == 1 | v20 ==0 | v85  == 1 | v86  == 1
	replace v180 = 1 if v18 ==0
	
* SET DEFAULTED BORROWERS SCHEDULED AMOUNT TO MISSING (ENTIRE BALANCE BECOMES DUE WHEN DEFAULT LEAVING SOME CRAZY VALUES
	replace v18 = . if v87 ==1
	replace v180 = . if v87 == 1	

	drop if v58==1
	
* AUTO DEBIT INDICATOR
	gen auto_debit = v88==1
	
* CHANGED SERVICERS
	recode v89 (0 = .)
	gen svc_chg = v89 ~= v46 if ~inlist(v46,.,0,555,43)  & ~inlist(v89,.,0,555,43)
	
	foreach v of var  v90 /*F4C*/ v87 /*F5B*/ any_forb /*F6C*/ v18 /*F9*/ auto_debit /*F10C*/ v180 /*FA9*/ v20 /*FA10*/ {
	
	loc controls = "Yappyr_2016 Yappyr_2017 Yappyr_2018"
	
	if "`v'" == "outst_bal" {
		loc controls = "Yappyr_2016 Yappyr_2017 Yappyr_2018 v45"
	}
		reg `v' v56 v57 v4  `controls' , robust
		
		loc ap = "ap"
		
		if `n' == `start' {
			loc ap = "replace"
		}
		
		outreg2 using "${output}\monthly_`v'.txt", keep(v56 v57) `ap' addtext("parm",coef_`n') st(coef ) noaster nonotes nopa nocons nor 
		outreg2 using "${output}\monthly_`v'.txt", keep(v56 v57) ap addtext("parm",cil_`n') st(ci_low ) noaster nonotes nopa nocons nor 
		outreg2 using "${output}\monthly_`v'.txt", keep(v56 v57) ap addtext("parm",cih_`n') st(ci_high ) noaster nonotes nopa nocons nor 
	
	}
	
* CLOSE LOOP OVER MONTHS SINCE INITIAL APP
}


/*********************************************************************
	* 2) F10A-B SCATTER PLOTS FOR AUTO DEBIT FOR SUBSET OF PERIODS
*********************************************************************/
foreach n of num -1 12 {

* SERVICER ID AND AUTO DEBIT IND
	unzipfile     "idrrd_evar1_moel`n'.csv.zip" , replace
	insheet using "idrrd_evar1_moel`n'.csv", names clear 
	erase         "idrrd_evar1_moel`n'.csv"	
	keep v5 v89 v88
	tempfile temp
	save `temp', replace

* MAIN MONTHLY FILE
	unzipfile     "idrrd_moel_`n'.csv.zip" , replace
	insheet using "idrrd_moel_`n'.csv", names clear 
	erase         "idrrd_moel_`n'.csv"

	merge 1:1 v5 using `temp', keep(3) nogen
	
* WINSORIZE MONTHLY SCHEDULED PAYMENT AT 99TH PERCENTILE
	sum v18, det
	replace v18 = r(p99) if v18>r(p99) & v18<.	
	
* INFLATION ADJ RUNNING VAR
	drop v4 v58
	merge 1:1 v5 using "${working}\treatment", keep(3) nogen

* DROP OBS FROM 2014
	drop if v23 == 2014

	merge m:1 v5 using "${working}\baseline", keep(1 3) nogen

	drop if v45 <500
	
* AUTO DEBIT INDICATOR
	gen auto_debit = v88==1
		
	gen bin = round(v4 +124, 250) - 125
	gen num = 1
	collapse (mean) auto_debit (sum) num, by(bin v56 )
	drop if abs(bin)>6000

	tw  (sc auto_debit bin , ms(D) mc(ebblue%75)) (lfitci auto_debit bin if d==1 & bin >-4000 , ciplot(rline) clc(ebblue) alc(emidblue) alp(longdash) alw(thin)) ///
		(lfitci auto_debit bin if d==0 & bin >125 & bin<4000 , ciplot(rline ) clc(ebblue) alc(emidblue) alp(longdash) alw(thin)) if bin ~=125 & abs(bin )<6000, ///
		legend(off) graphr(fc(white) margin(small)) note("") xli(0, lc(black) lp(shortdash)) yti(Enrolled in auto debit) xlab(-6000(1000)6000, labs(small) angle(45)) xti(Distance from 150% FPL Threshold) xsiz(7)
		
	graph save "${output}\F10scatter_auto_debit_`n'.gph", replace
}

/**********************************************************************************/
* 3) SCATTER PLOTS (FS, REPAYMENT STATS, DELINQ, DEFAULTS) FOR SUBSET OF PERIODS 
* (F3A-B, FA5, FA6, F4A-B, F5A, F6A-B, FA7) 
/**********************************************************************************/

foreach n of num 6 9 12 18 24 36 {

* SERVICER ID AND AUTO DEBIT IND
	unzipfile     "idrrd_evar1_moel`n'.csv.zip" , replace
	insheet using "idrrd_evar1_moel`n'.csv", names clear 
	erase         "idrrd_evar1_moel`n'.csv"	
	keep v5 v89 v88
	tempfile temp
	save `temp', replace

* MAIN MONTHLY FILE
	unzipfile     "idrrd_moel_`n'.csv.zip" , replace
	insheet using "idrrd_moel_`n'.csv", names clear 
	erase         "idrrd_moel_`n'.csv"

	merge 1:1 v5 using `temp', keep(3) nogen

* WINSORIZE MONTHLY SCHEDULED PAYMENT AT 99TH PERCENTILE
	sum v18, det
	replace v18 = r(p99) if v18>r(p99) & v18<.	
		
* INFLATION ADJ RUNNING VAR
	drop v4  v58
	merge 1:1 v5 using "${working}\treatment", keep(3) nogen

	merge m:1 v5 using "${working}\baseline", keep(1 3) nogen

* SAMPLE SELECTION
	drop if v45 <500
	drop if v46 == 2
	
* BANDWIDTH	
	keep if abs(v4)<6500

	
	foreach v of var v18 v20 /*v91 v92 */ {
		replace `v' = round(`v'*cpi)
	}
		
* INCREASE IN OUTSTANDING DEBT (NOMINAL) SINCE BASELINE? 
	egen dum = rowtotal(v91 v92)
	gen bal_increase = round(dum,100) > round(outst_bal_nom0,100)
		
* ANY FORB
	egen any_forb = rowtotal(v79 v80 v81) if v79 ~=. | v80 ~= . | v81 ~= . 
	
* RECODE MISSING SCHEDULED AMOUNT IF IN STATUSES WE KNOW HAVE $0 PAYMENT	
	recode v18 (. = 0) if v82 == 1 | v83 == 1 | v84 == 1 | v79 ==1  | v81 ==1 | v80 == 1 ///
		| v84 == 1 | v20 ==0 | v85  == 1 | v86  == 1
	replace v180 = 1 if v18 ==0
	
* SET DEFAULTED BORROWERS SCHEDULED AMOUNT TO MISSING (ENTIRE BALANCE BECOMES DUE WHEN DEFAULT LEAVING SOME CRAZY VALUES
	replace v18 = . if v87 ==1
	replace v180 = . if v87 == 1	

	drop if v58 ==1
	
* AUTO DEBIT INDICATOR
	gen auto_debit = v88==1
	
* CHANGED SERVICERS
	recode v89 (0 = .)
	gen svc_chg = v89 ~= v46 if ~inlist(v46,.,0,555,43)  & ~inlist(v89,.,0,555,43)
	

* $250 BINS
	gen bin = round(v4 + 124, 250) - 125

	replace v18 = v18*12
	egen forb = rowmax(v79 v80 v81)
	gen num = 1
	
	collapse (mean) v18 v180 v87 v90 v20 forb (sum) num, by(moel bin d)

	if `n' == 9 {
		loc ti = ""
	}
	if `n'~=9 {
		loc ti = "`n' months after initial IDR app"
	}
	** F3B, FA6: FIRST STAGE, 9 MONTHS AFTER INITIAL APP
	* SCHEDULED PAYMENT (ANNUALIZED)
	tw  (sc v18 bin , ms(D) mc(midblue%75))  ///
		(lfitci v18 bin if d==1 & bin >-4000, ciplot(rline) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
		(lfitci v18 bin if d==0 & bin >125 & bin<4000, ciplot(rline ) clc(black) alc(gs10) alp(longdash) alw(thin) ) /// 
			if bin ~=125 & abs(bin )<6000, ///
		xli(0, lc(black) lp(shortdash)) xlab(-6000(1000)6000, labs(small)) xti(Distance from 150% FPL Threshold) ///
		yti("Scheduled payment (annualized)") ///
		legend(off) graphr(fc(white) margin(small)) ti("`ti'") xsize(6.25)
		
	 graph save "${output}\F3scatter_v18_`n'.gph", replace

	 * F3A, FA5: $0 PAYMENT
	 tw (sc v180 bin ,  mc(orange%80)) ///
		(lfitci v180 bin if d==1 & bin >-4000, ciplot(rline) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
		(lfitci v180 bin if d==0 & bin >125 & bin<4000, ciplot(rline ) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
		if bin ~=125 & abs(bin )<6000,  ///
		xli(0, lc(black) lp(shortdash)) xlab(-6000(1000)6000, labs(small)) xti(Distance from 150% FPL Threshold) ///
		yti("Scheduled payment = $0") ylab( , gmin gmax)  ///
		legend(off) graphr(fc(white) margin(small)) ti("`ti'") xsize(6.25)
		
	 graph save "${output}\F3scatter_v180_`n'.gph", replace
	 
	** REDUCED FORM OUTCOMES
	* F4A-B: DELINQUENCY
	tw  (sc v90 bin,  mc(erose%80) ms(S)) ///
		(lfitci v90 bin if d==1 & bin >-4000, ciplot(rline) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
		(lfitci v90 bin if d==0 & bin >125 & bin<4000, ciplot(rline ) clc(black) alc(gs10) alp(longdash) alw(thin))  ///
		if bin ~=125 & abs(bin )<6000, ///
		xli(0, lc(black) lp(shortdash)) xlab(-6000(1000)6000, labs(small)) xti(Distance from 150% FPL Threshold) ///
		yti("Percent 30+ days delinquent") ylab(   , gmin gmax) ///
		legend(off) graphr(fc(white) margin(small))  xsize(6.25)
	 graph save "${output}\F4scatter_delinq_`n'.gph", replace

	* F5A, FA7: DEFAULT
	tw  (sc v87  bin,  mc(cranberry%80) ms(S)) ///
		(lfitci v87 bin if d==1 & bin >-4000, ciplot(rline) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
		(lfitci v87  bin if d==0 & bin >125 & bin<4000, ciplot(rline ) clc(black) alc(gs10) alp(longdash) alw(thin))  ///
			if bin ~=125 & abs(bin )<6000, ///
		xli(0, lc(black) lp(shortdash)) xlab(-6000(1000)6000, labs(small))   ///
		yti("Percent in default") ylab(  , gmin gmax) ///
		legend(off) graphr(fc(white)) xti(Distance from 150% FPL Threshold) 
	 graph save "${output}\F5scatter_v87_`n'.gph", replace

	* F6A-B: FORBS
	tw  (sc forb bin, mc(purple%80) ms(T)) ///
		(lfitci forb  bin if d==1 & bin >-4000, ciplot(rline) clc(black) alc(gs10) alp(longdash) alw(thin)) ///
		(lfitci forb bin if d==0 & bin >125 & bin<4000, ciplot(rline ) clc(black) alc(gs10) alp(longdash) alw(thin))  ///
		if bin ~=125 & abs(bin )<6000, ///
		xli(0, lc(black) lp(shortdash)) xlab(-6000(1000)6000, labs(small)) xti(Distance from 150% FPL Threshold) /// 
		yti("Percent in Forbearance") ylab( , gmin gmax) ///
		legend(off) graphr(fc(white) margin(small)) xsize(6.25) 
	 graph save "${output}\F6scatter_forb_`n'.gph", replace	 
 
* CLOSE LOOP OVER SELECTED v3
}

/**********************************************************************************/
/**********************************************************************************/
**# T3, T4, T5, T6
*** FS, RF, AND IV ESTIMATES FOR ANNUAL MEASURES 
/**********************************************************************************/
/**********************************************************************************/

/*********************************************************************
	* 1) T3 FS REGS (LOCAL LINEAR, UNIFORM KERNEL)
*********************************************************************/
use "${working}\collapse", clear

** SAMPLE SELECTION
* DROP OBS WITH ORIGINAL OUTSTANDING BALANCE < $500
drop if v45 < 500
drop if v46 == 2

* PRE-PERIOD AVERAGES
foreach v in av_amt total_amt pct_sched0  any_sched0 {	
	egen `v'_pre = rowmean(`v'_0 `v'_1)
	gen m_`v'_pre = `v'_pre == .
	recode `v'_pre (. =0 )
}

** CUMULATIVE PAYMENTS, MONTHS OF $0 TD
* PRE-PERIOD MEASURES
egen total_amt_td_pre = rowtotal(total_amt_0 total_amt_1) 
gen m_total_amt_td_pre = total_amt_0 == . & total_amt_1 ==.
egen months_zero_pre = rowtotal(months_sched0_0 months_sched0_1 )
gen m_months_zero_pre = months_sched0_0 ==. & months_sched0_1 ==.

* POST-PERIOD MEASURES
gen total_amt_td_2 = total_amt_2
gen months_zero_2 = months_sched0_2
 
forv i = 3/8 {
	loc j = `i'-1
	egen total_amt_td_`i' = rowtotal(total_amt_td_`j' total_amt_`i') 
	egen months_zero_`i' = rowtotal(months_zero_`j' months_sched0_`i')
}

** WINSORIZE AMOUNTS AT 99TH PERCENTILE
* PRE-PERIOD MEASURES
foreach v in av_amt total_amt total_amt_td {
	sum `v'_pre, det
	replace `v'_pre = r(p99) if `v'_pre > r(p99) & `v'_pre <. 
}

* POST-PERIOD MEASURES
forv i = 2/8 {
	
foreach v in av_amt total_amt total_amt_td {
	sum `v'_`i', det
	replace `v'_`i' = r(p99) if av_amt_`i'>r(p99) & av_amt_`i'<.
}

}

** ANNUAL AMOUNTS 
egen av_amt_y1 = rowmean(v59 v60) if v59 ~=. | v60 ~=.
egen av_amt_y2 = rowmean(v61 v62)  if v62 ~=.
egen av_amt_y3 = rowmean(v63 v64) if v64 ~=.
egen any_sched0_y1 = rowmax(any_sched0_2 any_sched0_3) if v59 ~=. | v60 ~=.
egen any_sched0_y2 = rowmax(any_sched0_4 any_sched0_5) if v62 ~=.
egen any_sched0_y3 = rowmax(any_sched0_6 any_sched0_7) if v64 ~=.

gen in_samp = v59 ~=. | v60 ~=.

gen in_samp_y1 = v60 ~=.
gen in_samp_y2 = v62 ~=.
gen in_samp_y3 = v64 ~=.

set seed 24681012
loc varli = "av_amt_ any_sched0_"

loc j = 0

foreach v in `varli' {
	
forv i = 1/1 {

loc j = `j'+1

if `i'<=2 {
	loc cont = "Yappyr_2016 Yappyr_2017 Yappyr_2018" 
}

if `i'==3 {
	loc cont = 	"Yappyr_2016 Yappyr_2017" 
}

** T3: FIRST STAGE EFFECTS ON PAYMENTS
* MAIN SPEC 
	reg `v'y`i' v56 v57 v4 `cont' if abs(v4)<(4.35) & v58  ==0 & in_samp==1, robust
	test v56 = v57
	loc p = r(p)
	sum `v'y`i' if e(sample) == 1 & v56 ==0
	loc m = r(mean)
	loc ap = "ap"
	if `j' == 1 {
		loc ap = "replace"
	}
	outreg2 using "${output}\T3FS_point_ests_ann.txt", keep(v56 v57) ap addstat("depvar mean | abv threshold",`m',"pval",`p') addtext("specification","excluding servicer id = 2") aster(se) 10pct dec(3)	
	
}

}
	
	
/*********************************************************************
	* 2) T4: RF REGS - DELINQ, DEF, FORB (ANNUAL + EVER TD) 
*********************************************************************/
set seed 24681012
use "${working}\collapse", clear


** SAMPLE SELECTION
* DROP OBS WITH ORIGINAL OUTSTANDING BALANCE < $500
drop if v45 < 500
drop if v46 == 2

** ANY FORB
forv i = 0/8 {
	egen any_forb_any_`i' = rowmax(any_forb_dc_`i' any_forb_ma_`i' any_forb_ad_`i') ///
	if any_forb_dc_`i' ~=. | any_forb_ad_`i' ~=. | any_forb_ma_`i' ~=.
	rename pct_forbany_`i' pct_forb_any_`i'
}

** DELINQUENCY AND DEFAULT SUMMARY MEASURES
foreach s in deflt delinq forb_any {
	egen ever_`s'_3 = rowmax(any_`s'_2 any_`s'_3) if any_`s'_2 ~=. | any_`s'_3 ~=.
	gen dum2 = pct_`s'_2*num_months_2 
	gen dum3 = pct_`s'_3*num_months_3
	egen c_months_`s'_3 = rowtotal(dum2 dum3)
	drop dum2 dum3

forv i = 3/7 {

loc j = `i'+1
	
	egen ever_`s'_`j' = rowmax(ever_`s'_`i' any_`s'_`j') if ever_`s'_`i'~=. | any_`s'_`j' ~=.
	gen dum`j' = pct_`s'_`j'*num_months_`j'
	egen c_months_`s'_`j' = rowtotal(c_months_`s'_`i' dum`j')
	drop dum`j'
}


* PRE-APPLICATION MEASURES OF OUTCOMES
	egen any_`s'_pre = rowmax(any_`s'_0 any_`s'_1)
	egen pct_`s'_pre = rowmax(pct_`s'_0 pct_`s'_1)
	gen dum0 = num_months_0*pct_`s'_0
	gen dum1 = num_months_1*pct_`s'_1
	egen c_months_`s'_pre = rowtotal(dum0 dum1)
	drop dum0 dum1
	gen ever_`s'_pre = any_`s'_pre

}

* OUTCOMES MEASURED OVER 12 MO PERIOD
foreach v in any_delinq_ any_forb_any_ {
	egen `v'y1 = rowmax(`v'2 `v'3) 
	egen `v'y2 = rowmax(`v'4 `v'5) if  `v'5 ~=.
	egen `v'y3 = rowmax(`v'6 `v'7) if  `v'7 ~=.
}

foreach v in ever_delinq_ ever_forb_any_ {
	rename `v'3 `v'y1
	rename `v'5 `v'y2
	rename `v'7 `v'y3
	
}

** SEPARATELY FOR DEFAULT (LAGS 8 MONTHS)
foreach v in any_deflt_ {
	egen `v'y1 = rowmax(`v'3 `v'4) 
	egen `v'y2 = rowmax(`v'5 `v'6) if  `v'5 ~=.
	egen `v'y3 = rowmax(`v'7 `v'8) if  `v'7 ~=.
}

foreach v in ever_deflt_ {
	rename `v'4 `v'y1
	rename `v'6 `v'y2
	rename `v'8 `v'y3
	
}

forv i = 1/3 {

foreach t in delinq deflt forb_any {
	replace ever_`t'_y`i' = . if any_`t'_y`i'==.
}
}

gen in_samp = v59 ~=. | v60 ~=.

** FIXED BW REGRESSIONS
loc j = 0	

foreach v in any_delinq_ ever_delinq_ any_deflt_ ever_deflt_ any_forb_any_ ever_forb_any_ {

forv i = 1/3 {

loc j = `j'+1
	
loc cont = "Yappyr_2016 Yappyr_2017 Yappyr_2018"

if `i' == 3 {
	loc cont = "Yappyr_2016 Yappyr_2017"

}
			
* REGRESSIONS
loc j = `j'+1
		
	reg `v'y`i' v56 v57 v4 `cont' if abs(v4)<(4.35) & v58 == 0 & in_samp == 1, robust
	sum `v'y`i' if e(sample) == 1 & v56 == 0
	loc m = r(mean)
	loc ap = "ap"
	if `j' == 1 {
		loc ap = "replace"
	}
	outreg2 using "${output}\T4RF_point_ests_fixedbw_del_def_forb.txt", keep(v56 v57) `ap' addstat("depvar mean",`m') aster(se) 10pct dec(4)

* CLOSE LOOP OVER DEPVARS
}

* CLOSE LOOP OVER RF OUTCOMES
}

/*********************************************************************
	* 3) T5: RF REGS - IDR RECERT  (ANNUAL) 
*********************************************************************/
set seed 24681012
use "${working}\collapse", clear

** SAMPLE SELECTION
* DROP OBS WITH ORIGINAL OUTSTANDING BALANCE < $500
drop if v45 < 500
drop if v46 ==2


* OUTCOMES MEASURED OVER 12 MO PERIOD
foreach v in any_idr_ auto_debit {
	egen `v'y1 = rowmax(`v'2 `v'3) 
	egen `v'y2 = rowmax(`v'4 `v'5) if `v'5 ~=.
	egen `v'y3 = rowmax(`v'6 `v'7) if `v'7 ~=.
}

foreach v in any_app_ any_valid_app_ {
	egen `v'td_y1 = rowmax(`v'2 `v'3) 
	egen `v'td_y2 = rowmax(`v'2 `v'3 `v'4 `v'5) if `v'5 ~=.
	egen `v'td_y3 = rowmax(`v'2 `v'3 `v'4 `v'5 `v'6 `v'7) if `v'7 ~=.
	
}



gen in_samp = v59 ~=. | v60 ~=.
	
loc j = 0	

foreach v of var any_idr_y1 any_idr_y2 any_idr_y3 any_app_td_y1 any_app_td_y2 any_app_td_y3 ///
	any_valid_app_td_y1 any_valid_app_td_y2 any_valid_app_td_y3  {

loc j = `j'+1
	
loc cont = "Yappyr_2016 Yappyr_2017 Yappyr_2018"

if "`v'"  == "any_idr_y3" | "`v'" == "any_app_td_y3" | "`v'" == "any_valid_app_td_y3" ///
	| "`v'" == "auto_debity3" {
	loc cont = "Yappyr_2016 Yappyr_2017"
}

loc j = `j'+1
		
	reg `v' v56 v57 v4  `cont' if abs(v4)<(4.35) & v58  ==0 , robust
	sum `v' if e(sample) == 1 & v56 == 0
	loc m = r(mean)
	loc ap = "ap"
	if `j' == 1 {
		loc ap = "replace"
	}
	outreg2 using "${output}\T5RF_point_ests_fixedbw_idr.txt",keep(v56 v57) `ap' addstat("depvar mean",`m') aster(se) 10pct dec(4)

* CLOSE LOOP OVER DEPVARS
}


/*********************************************************************
	* 4) OUTPUT FOR F11: HETEROGENEITY
*********************************************************************/
set seed 24681012
use "${working}\collapse", clear

** SAMPLE SELECTION
* DROP OBS WITH ORIGINAL OUTSTANDING BALANCE < $500
drop if v45 < 500
drop if v46 == 2

merge 1:1 v5 using "${working}\baseline", nogen keep(1 3)

	
* CREATE BINARY VARIABLES WITH DIMENSIONS OF HETEROGENEITY
gen grad = Amax_stud_a_6 ==1
gen firstyr = Amax_stud_a_1 == 1
gen other_attain = 1-grad - firstyr
gen nov44 = 1- v44

	
** DELINQUENCY AND DEFAULT SUMMARY MEASURES
foreach s in deflt delinq {
	egen ever_`s'_3 = rowmax(any_`s'_2 any_`s'_3) if any_`s'_2 ~=. | any_`s'_3 ~=.
	gen dum2 = pct_`s'_2*num_months_2 
	gen dum3 = pct_`s'_3*num_months_3
	egen c_months_`s'_3 = rowtotal(dum2 dum3)
	drop dum2 dum3

forv i = 3/7 {

loc j = `i'+1
	
	egen ever_`s'_`j' = rowmax(ever_`s'_`i' any_`s'_`j') if ever_`s'_`i'~=. | any_`s'_`j' ~=.
	gen dum`j' = pct_`s'_`j'*num_months_`j'
	egen c_months_`s'_`j' = rowtotal(c_months_`s'_`i' dum`j')
	drop dum`j'
}


* PRE-APPLICATION MEASURES OF OUTCOMES
	egen any_`s'_pre = rowmax(any_`s'_0 any_`s'_1)
	egen pct_`s'_pre = rowmax(pct_`s'_0 pct_`s'_1)
	gen dum0 = num_months_0*pct_`s'_0
	gen dum1 = num_months_1*pct_`s'_1
	egen c_months_`s'_pre = rowtotal(dum0 dum1)
	drop dum0 dum1
	gen ever_`s'_pre = any_`s'_pre

}

foreach v in deflt delinq  {
	gen ever_`v'_2 = any_`v'_2 
}

gen in_samp = v59 ~=. | v60 ~=.
	
	
* SET BW
loc bw = 4.35

* SET CONTROL VARS	
loc cont = "Yappyr_2016 Yappyr_2017 Yappyr_2018"


* BY RACE
foreach t in black hispanic white {
	
loc j = 0

* LOOP OVER FS & RF VARIABLES 
foreach v in any_delinq_ any_deflt_  ever_deflt_ ever_delinq_ {

forv i = 2/8 {
		
loc j = `j'+1

		
* RDD
	di "depvar = `v', het = `t'"
	reg `v'`i' v56 v57 v4  `cont' [aw = phat_`t'] if abs(v4)<(`bw') & v58  ==0 & in_samp==1 , robust
	sum `v'`i' if e(sample) == 1 & v56 ==0 [aw = phat_`t']
	loc m = r(mean)
	loc ap = "ap"
	if `j' == 1 {
		loc ap = "replace"
	}
	outreg2 using "${output}\RF_point_ests_het_`t'.txt", keep(v56 v57) `ap' addstat("depvar mean | above threshold",`m') aster(se) 10pct dec(3)

	
* CLOSE LOOP OVER DEPVARS
}

* CLOSE LOOP OVER PERIODS
}

* CLOSE LOOP OVER HET
}


* LOOP OVER DIMENSIONS OF HETEROGENEITY
foreach t in  grad other_attain firstyr v44 noprior_dflt  {
	

loc j = 0

* LOOP OVER FS & RF VARIABLES 
foreach v in any_delinq_ any_deflt_  ever_deflt_ ever_delinq_ {
forv i = 2/8 {

loc j = `j'+1

		
* RDD
	di "depvar = `v', het = `t'"
	reg `v'`i' v56 v57 v4 `cont' if abs(v4)<(`bw') & v58  ==0 & `t' == 1 & in_samp ==1, robust
	sum `v'`i' if e(sample) == 1 & v56 ==0
	loc m = r(mean)
	loc ap = "ap"
	if `j' == 1 {
		loc ap = "replace"
	}
	outreg2 using "${output}\RF_point_ests_het_`t'.txt", keep(v56 v57) `ap' addstat("depvar mean | above threshold",`m') aster(se) 10pct dec(3)

	
* CLOSE LOOP OVER DEPVARS
}

* CLOSE LOOP OVER PERIODS
}

* CLOSE LOOP OVER DIMENSIONS OF HET
}

/*********************************************************************
	* 5) T6, TA2: TREATMENT PARMS, IV-RD & IV-RK, TEST OF EQ 
*********************************************************************/
set seed 24681012
use "${working}\collapse", clear

** SAMPLE SELECTION
* DROP OBS WITH ORIGINAL OUTSTANDING BALANCE < $500
drop if v45 < 500

drop if v46 == 2

merge 1:1 v5 using "${working}\baseline", nogen keep(1 3)

* TREATMENT VARS: PR SCHEDULED $0 (any_sch0_3) AND AVERAGE MONTHLY PAYMENT (v60)
gen sch0 = any_sched0_3 
gen payment = v60/10

** ANY FORB
forv i = 0/8 {
	egen any_forb_any_`i' = rowmax(any_forb_dc_`i' any_forb_ma_`i' any_forb_ad_`i') ///
	if any_forb_dc_`i' ~=. | any_forb_ad_`i' ~=. | any_forb_ma_`i' ~=.
	rename pct_forbany_`i' pct_forb_any_`i'
}

** DELINQUENCY AND DEFAULT SUMMARY MEASURES
foreach s in deflt delinq forb_any {
	egen ever_`s'_3 = rowmax(any_`s'_2 any_`s'_3) if any_`s'_2 ~=. | any_`s'_3 ~=.
	gen dum2 = pct_`s'_2*num_months_2 
	gen dum3 = pct_`s'_3*num_months_3
	egen c_months_`s'_3 = rowtotal(dum2 dum3)
	drop dum2 dum3

forv i = 3/7 {

loc j = `i'+1
	
	egen ever_`s'_`j' = rowmax(ever_`s'_`i' any_`s'_`j') if ever_`s'_`i'~=. | any_`s'_`j' ~=.
	gen dum`j' = pct_`s'_`j'*num_months_`j'
	egen c_months_`s'_`j' = rowtotal(c_months_`s'_`i' dum`j')
	drop dum`j'
}


* PRE-APPLICATION MEASURES OF OUTCOMES
	egen any_`s'_pre = rowmax(any_`s'_0 any_`s'_1)
	egen pct_`s'_pre = rowmax(pct_`s'_0 pct_`s'_1)
	gen dum0 = num_months_0*pct_`s'_0
	gen dum1 = num_months_1*pct_`s'_1
	egen c_months_`s'_pre = rowtotal(dum0 dum1)
	drop dum0 dum1
	gen ever_`s'_pre = any_`s'_pre

}

* OUTCOMES MEASURED OVER 12 MO PERIOD
foreach v in any_delinq_  any_forb_any_ {
	egen `v'y1 = rowmax(`v'2 `v'3) 
	egen `v'y2 = rowmax(`v'4 `v'5) if  `v'5 ~=.
	egen `v'y3 = rowmax(`v'6 `v'7) if  `v'7 ~=.
}

foreach v in ever_delinq_ ever_forb_any_ {
	rename `v'3 `v'y1
	rename `v'5 `v'y2
	rename `v'7 `v'y3
	
}

* DO DEFAULTS SEPARATELY (Y1 = MO 6-18, Y2 = MO 19-30, Y3 = MO 31-42
foreach v in any_deflt_ {
	egen `v'y1 = rowmax(`v'3 `v'4) 
	egen `v'y2 = rowmax(`v'5 `v'6) if  `v'5 ~=.
	egen `v'y3 = rowmax(`v'7 `v'8) if  `v'7 ~=.
}

foreach v in ever_deflt_ {
	rename `v'4 `v'y1
	rename `v'6 `v'y2
	rename `v'8 `v'y3
	
}

forv i = 1/3 {

foreach t in delinq forb_any deflt {
	replace ever_`t'_y`i' = . if any_`t'_y`i'==.
}
}


* OUTCOMES MEASURED OVER 12 MO PERIOD
foreach v in any_idr_ auto_debit {
	egen `v'y1 = rowmax(`v'2 `v'3) 
	egen `v'y2 = rowmax(`v'4 `v'5) if `v'5 ~=.
	egen `v'y3 = rowmax(`v'6 `v'7) if `v'7 ~=.
}

foreach v in any_app_ any_valid_app_ {
	egen `v'td_y1 = rowmax(`v'2 `v'3) 
	egen `v'td_y2 = rowmax(`v'2 `v'3 `v'4 `v'5) if `v'5 ~=.
	egen `v'td_y3 = rowmax(`v'2 `v'3 `v'4 `v'5 `v'6 `v'7) if `v'7 ~=.
	
}

gen in_samp = payment ~=.


* SET BW
loc bw = 4.35

* SET CONTROL VARS	
loc cont = "Yappyr_2016 Yappyr_2017 Yappyr_2018"

** LOOP OVER OUTCOMES	
loc j = 0

foreach v in any_delinq_y1 any_delinq_y2 any_delinq_y3 ///
	any_deflt_y1 any_deflt_y2 any_deflt_y3  ///
	any_forb_any_y1 any_forb_any_y2 any_forb_any_y3 ///
	any_idr_y2 any_idr_y3 any_app_td_y1 any_app_td_y2 any_app_td_y3  {

	
loc j = `j'+1

	
	loc ap = "ap"
	if `j' == 1 {
		loc ap = "replace"
	}
		
	reg `v' v56 v57 v4  `cont' if abs(v4)<`bw' & v58  ==0 & in_samp ==1
	est store est_rf
	gen in_sample = e(sample) ==1
	
	reg payment v56 v57 v4  `cont' if abs(v4)<(`bw') & v58  ==0 & in_samp ==1
	est store est_fs

	drop in_sample 
	
	suest est_fs est_rf, rob
	
	nlcom [est_rf_mean]d/[est_fs_mean]d
	loc iv_rd_coef = el(r(table),1,1)
	loc iv_rd_se = el(r(table),2,1)
	loc iv_rd_p = el(r(table),4,1)
	
	nlcom [est_rf_mean]dd_inc/[est_fs_mean]dd_inc
	loc iv_rk_coef = el(r(table),1,1)
	loc iv_rk_se = el(r(table),2,1)
	loc iv_rk_p = el(r(table),4,1)
	
	testnl ([est_rf_mean]dd_inc/[est_fs_mean]dd_inc) = ([est_rf_mean]d/[est_fs_mean]d)
	loc test_rd_rk_eq = r(p)
	
	reg `v' if abs(v4)<`bw' & v58  ==0 & e(sample) == 1
	
	outreg2 using "${output}\T6_iv_test_eq_rdrk.txt", `ap' addstat("IV-RD coeff",`iv_rd_coef',"IV-RD se",`iv_rd_se', "IV-RD pval",`iv_rd_p', "IV-RK coeff",`iv_rk_coef',"IV-RK se", `iv_rk_se', "IV-RK pval",`iv_rk_p', "Test IV-RD = IV-RK pval",`test_rd_rk_eq') noaster dec(3)

	
* CLOSE LOOP OVER DEPVARS
}


*** T6: ISOLATE EFFECT OF NOT BEING REQUIRED TO MAKE PAYMENTS/DISCONNECTION EFFECT

* SET BW
loc bw = 4.35

* SET CONTROL VARS	
loc cont = "Yappyr_2016 Yappyr_2017 Yappyr_2018"

** LOOP OVER OUTCOMES	
loc j = 0

foreach v in any_delinq_y1 any_delinq_y2 any_delinq_y3 ///
	any_deflt_y1 any_deflt_y2 any_deflt_y3  ///
	any_forb_any_y1 any_forb_any_y2 any_forb_any_y3  ///
	any_idr_y2 any_idr_y3 any_app_td_y1 any_app_td_y2 any_app_td_y3    {

	
loc j = `j'+1

		
	reg `v' v56 v57 v4  `cont' if abs(v4)<`bw' & v58  ==0 & in_samp == 1
	est store est_rf
	gen in_sample = e(sample) ==1
	
	reg payment v56 v57 v4  `cont' if abs(v4)<(`bw') & v58  ==0 & in_sample ==1
	est store est_fs_p

	reg sch0 v56 v57 v4  `cont' if abs(v4)<(`bw') & v58  ==0 & in_sample ==1
	est store est_fs_p0
	
	drop in_sample 
	
	suest est_fs_p est_fs_p0 est_rf, rob
	
	nlcom ([est_rf_mean]d/[est_fs_p0_mean]d) - (([est_rf_mean]dd_inc/[est_fs_p_mean]dd_inc)*([est_fs_p_mean]d/[est_fs_p0_mean]d))
	loc d_coef = el(r(table),1,1)
	loc d_se = el(r(table),2,1)
	loc d_p = el(r(table),4,1)
	
	reg `v' if abs(v4)<`bw' & v58  ==0 & e(sample) == 1
	
	loc ap = "ap"
	if `j' == 1 {
		loc ap = "replace"
	}
	outreg2 using "${output}\T6_discon_te.txt", `ap' addstat("coeff",`d_coef',"se",`d_se', "pval",`d_p') noaster dec(3)

	
* CLOSE LOOP OVER DEPVARS
}







/**********************************************************************************/
/**********************************************************************************/
**# F4C, F5B, F6C, F7, F9, F10C, FA9, FA10, FA11
** MONTH BY MONTH RD ESTIMATE PLOTS
/**********************************************************************************/
/**********************************************************************************/
 
* LOOP OVER OUTCOMES
foreach v in  v18 /*F9*/ v180  /*FA9*/ v20 /*FA10*/ auto_debit /*F10C*/ ///
	v90 /*F4C*/ v87 /*F5B*/ any_forb  /*F6C*/   ///
	 any_enrl /*FA11A*/ anygrad /*FA11B*/ plan_idr_valid  /*F7*/ {

loc start = -12
loc F = ""

#d;

if  "`v'" == "v18" {; loc F = "9"; };
if  "`v'" == "sched_amt0" {; loc F = "A9"; };
if  "`v'" == "outst_bal" {; loc F = "A10"; };
if  "`v'" == "auto_debit" {; loc F = "10C"; };
if  "`v'" == "v90" {; loc F = "4C"; };
if  "`v'" == "v87" {; loc F = "5B"; };
if  "`v'" == "any_forb" {; loc F = "6C"; };
if  "`v'" == "any_enrl" {; loc start = 0; loc F = "A11A"; };
if  "`v'" == "anygrad"  {; loc start = 0; loc F = "A11B"; };
if  "`v'" == "plan_idr_valid" {; loc start = 0; loc F = "7"; };

#d cr

	
* RDD
	insheet using "${output}\monthly_`v'.txt", names clear
	keep if inlist(_n,3,7)
	drop v1
	gen num = _n
	reshape long v, i(num) j(j)
	reshape wide v, i(j) j(num)
	gen t = real(subinstr(substr(v2,-3,3),"_","",.))
	replace t = real(substr(v2,-1,1)) if t==.
	bysort t: gen num = _n
	drop j
	reshape wide v1 v2 , i(t) j(num)
	rename (v11 v12 v13) (coeff cil cih)
	keep t coeff cil cih
	replace cil = subinstr(cil,",","",.)
	replace cih = subinstr(cih,",","",.)
	destring *, replace

	tw (sc coeff t, mc(navy)) (rcap cil cih t , lc(midblue)) if t >=`start', ///
		graphr(fc(white) margin(small)) yli(0) xlab(`start'(2)46, labs(small) nogrid) ylab(, labs(small) gmin gmax) ///
		yti("Point Estimate, 95% CI") legend(off) xti("Months elapsed since initial IDR application") xsiz(8)

	graph save "${output}\F`F'monthly_rdplot_nosvc2_`v'.gph", replace

* CLOSE LOOP OVER OUTCOMES
}



/**********************************************************************************/
/**********************************************************************************/
**# F11: HETEROGENEITY FIGURES
/**********************************************************************************/
/**********************************************************************************/

/*********************************************************************
	* 1) PANEL A: RACE/ETHNICITY (PREDICTED PR)
*********************************************************************/
tempfile temp
loc j = 0

foreach t in black hispanic white {
	
loc j = `j'+1
	
	insheet using "${output}\RF_point_ests_het_`t'.txt", names clear
	keep if inlist(_n,1,3,4)
	drop v1
	gen num = _n
	reshape long v, i(num) j(j)
	reshape wide v, i(j) j(num)
	gen t = real(substr(v1,-1,1))
	replace v3 = subinstr(v3,"(","",.)
	replace v3 = subinstr(v3,")","",.)
	replace v3 = subinstr(v3,"+","",.)
	replace v3 = subinstr(v3,"*","",.)
	replace v3 = subinstr(v3,",","",.)
	destring v3, replace
	forv i = 1/8 {
		replace v1 = subinstr(v1,"`i'","",.)
	}
	rename (v1 v2) (var coeff)
	replace coeff = subinstr(coeff,",","",.)
	destring coeff, replace
	gen ci_l = coeff-(1.96*v3)
	gen ci_h = coeff+(1.96*v3)
	
	gen group = `j'
	
	if `j'>1 {
	append using `temp'
	}
	save `temp', replace
}	

gen t2 = t
gen t3 = t+.2
replace t = t-.2

tw  (sc coeff t if group == 1, mc(eltblue) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(eltblue) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(teal) ms(dh) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(teal) lw(thin)) ///
	(sc coeff t3 if group == 3, mc(khaki) ms(X) msiz(medlarge)) (rcap ci_l ci_h t3 if group == 3, lc(khaki) lw(thin) ) ///
	if var == "any_delinq_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.25(.05).05, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Delinquent) ///
	legend(lab(1 "") lab(2 Black) lab(3 "") lab(4 Hispanic) lab(5 "") lab(6 White) ///
		col(6) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Abiannual_het_race_any_delinq.gph", replace
	
tw  (sc coeff t if group == 1, mc(eltblue) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(eltblue) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(teal) ms(dh) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(teal) lw(thin)) ///
	(sc coeff t3 if group == 3, mc(khaki) ms(X) msiz(medlarge)) (rcap ci_l ci_h t3 if group == 3, lc(khaki) lw(thin) ) ///
	if var == "ever_delinq_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.25(.05).05, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Ever Delinquent) ///
	legend(lab(1 "") lab(2 Black) lab(3 "") lab(4 Hispanic) lab(5 "") lab(6 White) ///
		col(6) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Abiannual_het_race_ever_delinq.gph", replace
	
	
tw  (sc coeff t if group == 1, mc(eltblue) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(eltblue) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(teal) ms(dh) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(teal) lw(thin)) ///
	(sc coeff t3 if group == 3, mc(khaki) ms(X) msiz(medlarge)) (rcap ci_l ci_h t3 if group == 3, lc(khaki) lw(thin) ) ///
	if var == "any_deflt_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.04(.01).02, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Default) ///
	legend(lab(1 "") lab(2 Black) lab(3 "") lab(4 Hispanic) lab(5 "") lab(6 White) ///
		col(6) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Abiannual_het_race_any_deflt.gph", replace
	
tw (sc coeff t if group == 1, mc(eltblue) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(eltblue) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(teal) ms(dh) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(teal) lw(thin)) ///
	(sc coeff t3 if group == 3, mc(khaki) ms(X) msiz(medlarge)) (rcap ci_l ci_h t3 if group == 3, lc(khaki) lw(thin) ) ///
	if var == "ever_deflt_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.04(.01).02, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Ever Default) ///
	legend(lab(1 "") lab(2 Black) lab(3 "") lab(4 Hispanic) lab(5 "") lab(6 White) ///
		col(6) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Abiannual_het_race_ever_default.gph", replace
	
	
/*********************************************************************
	* 2) PANEL B: ATTAINMENT
*********************************************************************/
tempfile temp
loc j = 0

foreach t in firstyr other_attain grad {
	
loc j = `j'+1
	
	insheet using "${output}\RF_point_ests_het_`t'.txt", names clear
	keep if inlist(_n,1,3,4)
	drop v1
	gen num = _n
	reshape long v, i(num) j(j)
	reshape wide v, i(j) j(num)
	gen t = real(substr(v1,-1,1))
	replace v3 = subinstr(v3,"(","",.)
	replace v3 = subinstr(v3,")","",.)
	replace v3 = subinstr(v3,"+","",.)
	replace v3 = subinstr(v3,"*","",.)
	replace v3 = subinstr(v3,",","",.)
	destring v3, replace
	forv i = 1/8 {
		replace v1 = subinstr(v1,"`i'","",.)
	}
	rename (v1 v2) (var coeff)
	replace coeff = subinstr(coeff,",","",.)
	destring coeff, replace
	gen ci_l = coeff-(1.96*v3)
	gen ci_h = coeff+(1.96*v3)
	
	gen group = `j'
	
	if `j'>1 {
	append using `temp'
	}
	save `temp', replace
}	

gen t2 = t
gen t3 = t+.2
replace t = t-.2

tw (sc coeff t if group == 1, mc(lavender) ms(S) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(lavender) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(purple) ms(Th) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(purple) lw(thin)) ///
	(sc coeff t3 if group == 3, mc(pink) ms(V) msiz(medlarge)) (rcap ci_l ci_h t3 if group == 3, lc(pink) lw(thin) ) ///
	if var == "any_delinq_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.3(.05).05, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Delinquent) ///
	legend(lab(1 "") lab(2 "1st yr") lab(3 "") lab(4 "2nd-5th yr") lab(5 "") lab(6 "Grad borrower") ///
		col(6) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Bbiannual_het_attain_any_delinq.gph", replace
	
tw (sc coeff t if group == 1, mc(lavender) ms(S) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(lavender) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(purple) ms(Th) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(purple) lw(thin)) ///
	(sc coeff t3 if group == 3, mc(pink) ms(V) msiz(medlarge)) (rcap ci_l ci_h t3 if group == 3, lc(pink) lw(thin) ) ///
	if var == "ever_delinq_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.3(.05).05, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Ever Delinquent) ///
	legend(lab(1 "") lab(2 "1st yr") lab(3 "") lab(4 "2nd-5th yr") lab(5 "") lab(6 "Grad borrower") ///
		col(6) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Bbiannual_het_attain_ever_delinq.gph", replace
	
tw (sc coeff t if group == 1, mc(lavender) ms(S) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(lavender) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(purple) ms(Th) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(purple) lw(thin)) ///
	(sc coeff t3 if group == 3, mc(pink) ms(V) msiz(medlarge)) (rcap ci_l ci_h t3 if group == 3, lc(pink) lw(thin) ) ///
	if var == "any_deflt_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.06(.02).02, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Default) ///
	legend(lab(1 "") lab(2 "1st yr") lab(3 "") lab(4 "2nd-5th yr") lab(5 "") lab(6 "Grad borrower") ///
		col(6) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Bbiannual_het_attain_any_deflt.gph", replace
	
tw (sc coeff t if group == 1, mc(lavender) ms(S) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(lavender) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(purple) ms(Th) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(purple) lw(thin)) ///
	(sc coeff t3 if group == 3, mc(pink) ms(V) msiz(medlarge)) (rcap ci_l ci_h t3 if group == 3, lc(pink) lw(thin) ) ///
	if var == "ever_deflt_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.06(.02).02, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Ever Default) ///
	legend(lab(1 "") lab(2 "1st yr") lab(3 "") lab(4 "2nd-5th yr") lab(5 "") lab(6 "Grad borrower") ///
		col(6) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Bbiannual_het_attain_ever_default.gph", replace
	
	


/*********************************************************************
	* 3) PANEL C: PRIOR DEFAULT
*********************************************************************/
tempfile temp
loc j = 0

foreach t in v44 nov44 {
	
loc j = `j'+1
	
	insheet using "${output}\RF_point_ests_het_`t'.txt", names clear
	keep if inlist(_n,1,3,4)
	drop v1
	gen num = _n
	reshape long v, i(num) j(j)
	reshape wide v, i(j) j(num)
	gen t = real(substr(v1,-1,1))
	replace v3 = subinstr(v3,"(","",.)
	replace v3 = subinstr(v3,")","",.)
	replace v3 = subinstr(v3,"+","",.)
	replace v3 = subinstr(v3,"*","",.)
	replace v3 = subinstr(v3,",","",.)
	destring v3, replace
	forv i = 1/8 {
		replace v1 = subinstr(v1,"`i'","",.)
	}
	rename (v1 v2) (var coeff)
	replace coeff = subinstr(coeff,",","",.)
	destring coeff, replace
	gen ci_l = coeff-(1.96*v3)
	gen ci_h = coeff+(1.96*v3)
	
	gen group = `j'
	
	if `j'>1 {
	append using `temp'
	}
	save `temp', replace
}	

gen t2 = t+.1
replace t = t-.1

tw (sc coeff t if group == 1, mc(dknavy) ms(Dh) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(dknavy) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(ebblue) ms(O) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(ebblue) lw(thin)) ///
	if var == "any_delinq_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.25(.05).05, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Delinquent) ///
	legend(lab(1 "") lab(2 "Prior default") lab(3 "") lab(4 "No prior default") ///
		col(4) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Cbiannual_het_priordflt_any_delinq.gph", replace
	
tw (sc coeff t if group == 1, mc(dknavy) ms(Dh) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(dknavy) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(ebblue) ms(O) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(ebblue) lw(thin)) ///
	if var == "ever_delinq_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.25(.05).05, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Ever Delinquent) ///
	legend(lab(1 "") lab(2 "Prior default") lab(3 "") lab(4 "No prior default") ///
		col(4) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Cbiannual_het_priordflt_ever_delinq.gph", replace
	
	
tw (sc coeff t if group == 1, mc(dknavy) ms(Dh) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(dknavy) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(ebblue) ms(O) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(ebblue) lw(thin)) ///
	if var == "any_deflt_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.05(.01).01, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Default) ///
	legend(lab(1 "") lab(2 "Prior default") lab(3 "") lab(4 "No prior default") ///
		col(4) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Cbiannual_het_priordflt_any_deflt.gph", replace
	
tw (sc coeff t if group == 1, mc(dknavy) ms(Dh) msiz(medium)) (rcap ci_l ci_h t if group == 1, lc(dknavy) lw(thin)) ///
	(sc coeff t2 if group == 2, mc(ebblue) ms(O) msiz(medium)) (rcap ci_l ci_h t2 if group == 2, lc(ebblue) lw(thin)) ///
	if var == "ever_deflt_" , ///
	yli(0) yti("Point estimate, 95% CI") ylab(-.05(.01).01, gmin gmax labs(small)) ///
	xlab(2 "1-6" 3 "6-12" 4 "13-18" 5 "19-24" 6 "25-30" 7 "31-36" 8 "37-42", labs(small) nogrid) xti(Months since initial app) ///
	graphr(margin(small))  ti(Ever default) ///
	legend(lab(1 "") lab(2 "Prior default") lab(3 "") lab(4 "No prior default") ///
		col(4) size(small) position(6) region(lcolor(black) lw(vthin)) ) 
graph save "${output}\F11Cbiannual_het_priordflt_ever_deflt.gph", replace
	
		